<?php

namespace app\api\controller\minishop;

use app\common\controller\Api;
use app\api\model\minishop\Category as CategoryModel;

class Category extends Api
{
    // 不需要登录的接口
    protected $noNeedLogin = ['getCategories'];
    // 不需要鉴权的接口
    protected $noNeedRight = ['getCategories'];

    /**
     * 获取分类及二级分类接口
     *
     * @return \think\Response
     */
    public function getCategories()
    {
        // 获取所有一级分类
        $categories = CategoryModel::where('parent_id', 0)
            ->where('status', 1)
            ->field('id, title')
            ->order('id', 'asc')
            ->select();

        foreach ($categories as &$category) {
            // 获取该一级分类下的二级分类
            $category['foods'] = CategoryModel::where('pid', $category['id'])
                ->where('status', 1)
                ->field('title, key, icon, cat')
                ->order('id', 'asc')
                ->select();
        }

        // 返回接口响应
        $this->success('请求成功', $categories);
    }
}